package com.example.jh352160.surfingtv.mypage.childPage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.mypage.customView.MyPageSettingItem;
import com.example.jh352160.surfingtv.network.item.RequestItem;
import com.example.jh352160.surfingtv.network.bean.ChangePasswordBean;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.MyDataService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/27
 */

public class ChangePassword extends AppCompatActivity {

    MyPageSettingItem oldPassword;
    MyPageSettingItem newPassword;
    MyPageSettingItem identifyPassword;
    Button submit;
    Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_setting_change_password);
        oldPassword = (MyPageSettingItem) findViewById(R.id.oldPassword);
        newPassword = (MyPageSettingItem) findViewById(R.id.newPassword);
        identifyPassword = (MyPageSettingItem) findViewById(R.id.identifyPassword);
        submit = (Button) findViewById(R.id.submit);
        oldPassword.setView("原密码", "请输入原密码", InputType.TYPE_CLASS_TEXT);
        newPassword.setView("新密码", "请输入新密码", InputType.TYPE_CLASS_TEXT);
        identifyPassword.setView("确认新密码", "请再次输入新密码", InputType.TYPE_CLASS_TEXT);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText oldPasswordEdit = (EditText) oldPassword.findViewById(R.id.edittext);
                EditText newPasswordEdit = (EditText) newPassword.findViewById(R.id.edittext);
                EditText identifyPasswordEdit = (EditText) identifyPassword.findViewById(R.id.edittext);
                if (oldPasswordEdit.getText().toString().equals("") || newPasswordEdit.getText().toString().equals("") || identifyPasswordEdit.getText().toString().equals("")) {
                    Toast.makeText(ChangePassword.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (identifyPasswordEdit.getText().toString().equals(newPasswordEdit.getText().toString())) {
                    ChangePasswordBean changePasswordBean = new ChangePasswordBean(oldPasswordEdit.getText().toString(), newPasswordEdit.getText().toString());
                    Retrofit retrofit = CommonRetrofit.getRetrofit();
                    MyDataService myDataService = retrofit.create(MyDataService.class);
                    Call<RequestItem> call = myDataService.changePassword(changePasswordBean);
                    call.enqueue(new Callback<RequestItem>() {
                        @Override
                        public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                            if (response.body().getCode().equals("200")) {
                                ChangePassword.this.finish();
                                Toast.makeText(ChangePassword.this, "修改密码成功", Toast.LENGTH_LONG).show();

                            } else {
                                Toast.makeText(ChangePassword.this, "修改密码失败", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<RequestItem> call, Throwable t) {

                        }
                    });
                } else {
                    Toast.makeText(ChangePassword.this, "两次密码不同", Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}
